# -*- coding: utf-8 -*-

from sqlalchemy.dialects import mysql

from wxgame import db
from . import ts_func


class Gameset(db.Model):
    __tablename__ = 'gameset'

    id = db.Column(db.Integer, primary_key=True)
    sn = db.Column(mysql.BIGINT)
    player_count = db.Column(db.Integer, default=0)
    envelope_count = db.Column(db.Integer, default=0)
    envelope_amount = db.Column(db.Integer, default=0)
    wager_amount = db.Column(db.Integer, default=0)
    earned_amount = db.Column(db.Integer, default=0)
    deduction = db.Column(db.Integer, default=0)
    wager_begin_at = db.Column(db.Integer, default=ts_func)
    wager_end_at = db.Column(db.Integer)
    grab_begin_at = db.Column(db.Integer)
    grab_end_at = db.Column(db.Integer)
    status = db.Column(db.Integer, nullable=False, default=1)
    create_time = db.Column(db.Integer, nullable=False, default=ts_func)

    room_id = db.Column(db.String(32), db.ForeignKey('room.id'), nullable=True)
    banker_wxid = db.Column(db.String(32), db.ForeignKey('player.wxid'), nullable=True)

    banker = db.relationship(
        'Player',
        backref='hosted',
    )

    players = db.relationship(
        'PlayerGameset',
        backref=db.backref('gameset', lazy='joined'),
        lazy='dynamic'
    )

    transactions = db.relationship(
        'Transaction',
        backref='gameset',
        lazy='dynamic'
    )


class Transaction(db.Model):
    __tablename__ = 'transaction'

    id = db.Column(db.Integer, primary_key=True)
    game_phase = db.Column(db.String(16))
    amount = db.Column(db.Integer)
    msg = db.Column(db.String(2048))
    revoke_type = db.Column(db.Integer, default=0)
    revoke_info = db.Column(db.String(128))
    occurred_at = db.Column(db.Integer)
    # 0: revoked; 1: active
    status = db.Column(db.Integer, nullable=False, default=1)
    create_time = db.Column(db.Integer, nullable=False, default=ts_func)

    room_id = db.Column(db.String(32), db.ForeignKey('room.id'), nullable=True)
    player_wxid = db.Column(db.String(32), db.ForeignKey('player.wxid'), nullable=True)
    gameset_id = db.Column(db.Integer, db.ForeignKey('gameset.id'), nullable=True)
